package com.gs.SynPatrolRep.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.control.WEqualsHImageView;
import com.gs.SynPatrolRep.db.dto.CreatePalBillItemDto;
import com.gs.SynPatrolRep.dialog.CommDialogUtils;
import com.gs.SynPatrolRep.util.ImageUtil;
import com.gs.SynPatrolRep.util.OKHttpUtil;

import java.util.List;

public class PatrolItemPhotoAdapter extends RecyclerView.Adapter<PatrolItemPhotoAdapter.MyViewHolder> {

    private int countLimit;
    private Context context;
    private List<CreatePalBillItemDto.ExctPhoto> mDatas;
    private Activity activity;

    public PatrolItemPhotoAdapter(Activity activity, Context context, List<CreatePalBillItemDto.ExctPhoto> mDatas, int countLimit) {
        this.activity = activity;
        this.context = context;
        this.mDatas = mDatas;
        this.countLimit = countLimit;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rvitem_partolphoto, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final WEqualsHImageView ivPic = holder.ivPic;
        if (position == getItemCount() - 1 && mDatas.size() < countLimit) {
            holder.ivDelete.setVisibility(View.INVISIBLE);
            ivPic.setImageResource(R.mipmap.photo);
            ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CommDialogUtils.showCommboPicDialog(activity, "");
                }
            });
        } else {
            holder.ivDelete.setVisibility(View.VISIBLE);
            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delete(position);
                }
            });
            ImageUtil.getInstance().loadImg(context, mDatas.get(position).getPhoto(), ivPic);
            ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(v,position);
                    }
                }
            });
        }
    }

    public void delete(int position){
        mDatas.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,getItemCount()-position);
    }

    public void add(CreatePalBillItemDto.ExctPhoto bean){
        mDatas.add(bean);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (mDatas != null && mDatas.size() >= countLimit) {
            return countLimit;
        } else {
            return mDatas == null ? 1 : mDatas.size() + 1;
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private WEqualsHImageView ivPic;
        private ImageView ivDelete;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.iv_pic);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }

    protected OnItemClickListener onItemClickListener;//单击事件

    public interface OnItemClickListener {
        void onItemClickListener(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
